<template>
  <div id="add">
    <!-- 头部 -->
    <div class="navebar">
      <el-form
        :inline="true"
        :model="formInline"
        :rules="departRules"
        class="demo-form-inline"
      >
        <el-form-item label="合作商搜索">
          <el-input
            v-model="formInline.taskCode"
            placeholder="请输入"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">
            查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格新建按钮 -->
    <div class="box">
      <div class="btn">
        <el-button
          type="primary"
          icon="el-icon-circle-plus-outline"
          class="btn1"
          @click="centerDialogVisible = true"
        >新建</el-button>
      </div>

      <!-- 表格 -->
      <div class="tabel">
        <el-table
          v-loading="loading"
          :data="SearchList"
          style="width: 100%"
          height="419"
          :header-cell-style="{
            //标题头部的颜色
            background: '#f5f7fa',
            height: '40px',
          }"
        >
          <el-table-column fixed type="index" label="序号" width="90" />
          <el-table-column prop="name" label="合作商名称" width="140" />
          <el-table-column prop="account" label="账号" width="150" />
          <el-table-column prop="vmCount" label="设备数量" width="145" />
          <el-table-column label="分成比例" width="145">
            <template #default="{ row }">
              {{ row.ratio + "%" }}
            </template>
          </el-table-column>
          <el-table-column prop="contact" label="联系人" width="150" />
          <el-table-column prop="mobile" label="联系电话" width="140" />
          <el-table-column fixed="right" label="操作" width="240">
            <template slot-scope="scope">
              <el-button type="text" size="medium" @click="newPassword(scope.row.id)">重置密码</el-button>
              <el-button type="text" size="medium" @click="viewDetails(scope.row)">查看详情</el-button>
              <el-button
                type="text"
                size="medium"
                @click="alter(scope.row)"
              >修改</el-button>
              <el-button
                type="text"
                size="medium"
                style="color: red"
                @click="del(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 实现分页
        current-page 当前页数
        page-size 每页显示个数
        total 总条目数
        current-change 页面改变时会触发
        prev-text="上一页"
        next-text="下一页"
         -->
        <div class="block">
          <el-pagination
            background
            :current-page.sync="pageOption.pageIndex"
            :page-size="pageOption.pagesize"
            layout="total,  pager, prev, next"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <!-- 新建弹出框 -->
    <el-dialog
      title="新增区域"
      :visible.sync="centerDialogVisible"
      width="50%"
      :before-close="closeFn"
    >
      <!-- 表单内容 -->
      <el-form
        ref="departRef"
        :model="departForm"
        :rules="departRules"
        label-width="120px"
      >
        <el-form-item label="合作商名称:" prop="name">
          <el-input
            v-model="departForm.name"
            style="width: 85%"
            placeholder="请输入"
            maxlength="10"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="联系人:" prop="contact">
          <el-input
            v-model="departForm.contact"
            style="width: 85%"
            placeholder="请输入"
            maxlength="10"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input
            v-model="departForm.mobile"
            style="width: 85%"
            placeholder="请输入"
            maxlength="11"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="分成比例(%):" prop="ratio">
          <template>
            <el-input-number v-model="departForm.ratio" controls-position="right" :min="1" :max="100" style="width: 85%" placeholder="请输入" />
          </template>
        </el-form-item>
        <el-form-item v-if="!departForm.id" label="账号:" prop="account">
          <el-input
            v-model="departForm.account"
            style="width: 85%"
            placeholder="请输入"
            maxlength="18"
            show-word-limit
          />
        </el-form-item>
        <el-form-item v-if="!departForm.id" label="密码:" prop="password">
          <el-input
            v-model="departForm.password"
            style="width: 85%"
            placeholder="请输入"
            maxlength="18"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <!-- 底部按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button class="btnquxiao" @click="closeFn">取 消</el-button>
        <el-button
          type="primary"
          class="btn1"
          @click="submitWork"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 合作商详情弹窗 -->
    <div class="view">
      <el-dialog
        title="合作商详情"
        :visible.sync="partnerDetails"
        width="40%"
        @close="partnerDetails=false"
      >
        <el-descriptions :column="2">
          <el-descriptions-item label="合作商名称">{{ view.name }}</el-descriptions-item>
          <el-descriptions-item label="联系电话"> {{ view.mobile }}</el-descriptions-item>
          <el-descriptions-item label="联系人信息">{{ view.contact }}</el-descriptions-item>
          <el-descriptions-item label="分成比例">{{ view.ratio }}%</el-descriptions-item>
        </el-descriptions>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPenterListApi, addPenterApi, alterPenterApi, delPenterApi, servePenterApi } from '@/api'
import { Message } from 'element-ui'
import dayjs from 'dayjs'
export default {
  data() {
    return {
      loading: false,
      // 添加弹出框
      centerDialogVisible: false,
      partnerDetails: false,
      SearchList: [],
      pageOptions: {},
      currentPage1: 10,
      total: 0,
      num: 0, // 分成比例
      // 搜索框
      formInline: {
        taskCode: ''
      },
      // 新增区域数据
      departForm: {
        name: '',
        account: '',
        contact: '',
        mobile: '',
        password: '',
        ratio: ''
      },
      // 查看详情数据
      view: {},
      // 表单校验
      departRules: {
        name: [{ required: true, message: '合作商名称不能为空', trigger: ['blur'] }],
        account: [{ required: true, message: '请填写账号', trigger: ['blur'] }],
        contact: [{ required: true, message: '请填写联系人', trigger: ['blur'] }],
        ratio: [{ required: true, message: '请添加分成比例', trigger: ['blur'] }],
        mobile: [{ required: true, message: '请输入手机号', trigger: ['blur'] }],
        password: [{ required: true, message: '请填写密码', trigger: ['blur'] }]
      },
      // 搜索传入的数值
      pageOption: {
        pageIndex: 1,
        pagesize: 10000
      }

    }
  },
  async created() {
    this.getPenterList()
  },
  methods: {
    // 合作商搜索封装
    async getPenterList() {
      const { data } = await getPenterListApi({
        pageIndex: 1,
        pageSize: 10
      })
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
      console.log(data)
    },

    // 查询功能
    // 绑定查询事件，在下面声明点击事件
    // 封装数据请求接口、引入、使用
    // 点击查询按钮，将数据传给子组件表格
    // 子组件接收，并进行数据请求实现查询功能
    async onSubmit() {
      if (!this.formInline.taskCode) return Message.warning('请输入合作商名字')
      const { data } = await getPenterListApi({ ...this.pageOption, name: this.formInline.taskCode })
      this.SearchList = data.currentPageRecords
      this.total = +data.totalCount
    },

    // 新建弹窗用于合作商提交和修改事件
    // 准备新建弹窗dom结构
    // 封装接口，引入接口
    // 注册新建按钮和修改按钮的点击事件，点击打开合作商提交弹窗
    // 根据点击修改与新增按钮判断departForm里面是否有id来实现数据回显
    // 对弹窗内的input框进行表单校验
    // 在点击确定的时进行兜底校验
    // 请求接口，传入数据
    // 提示用户、关闭弹窗、清空表单
    async submitWork() {
      this.$refs.departRef.validate(async(vaild) => {
        if (!vaild) return
        if (this.departForm.id) {
          await alterPenterApi(this.departForm)
          Message.success('修改合作商成功')
        } else {
          await addPenterApi(this.departForm)
          Message.success('新增合作商成功')
        }
        this.getPenterList()
        this.centerDialogVisible = false
        this.departForm = {
          name: '',
          account: '',
          contact: '',
          mobile: '',
          password: '',
          ratio: ''
        }
      })
    },

    // 重置密码
    // 点击重置密码按钮，二次询问
    // 引入接口，调用传入id
    // 重置密码成功，提示用户
    // 重新渲染页面
    newPassword(id) {
      console.log(id)
      this.$confirm('此操作将会重置合作商密码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await servePenterApi(id)
        this.getPenterList()
        Message.success('重置密码成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        })
      })
    },

    // 查看详情按钮
    viewDetails(row) {
      this.partnerDetails = true
      console.log(row)
      this.view = row
    },

    // 修改按钮
    // 点击修改按钮
    // 展示修改按钮弹窗组件
    // 传入当前行的数据
    // 实现数据回显
    alter(val) {
      this.centerDialogVisible = true
      console.log(val)
      this.departForm = val
    },

    // 删除
    del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPenterApi(id)
        this.getPenterList()
        Message.success('删除成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 点击取消关闭弹窗、清除表单
    closeFn() {
      this.centerDialogVisible = false
      this.partnerDetails = false
      this.$refs.departRef.resetFields()
      this.departForm = {
        name: '',
        account: '',
        contact: '',
        mobile: '',
        password: '',
        ratio: ''
      }
    },

    // 格式化时间
    formatDate(value) {
      return dayjs(value).format('YYYY-MM-DD hh:mm:ss')
    },

    // 实现分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.loading = true
      this.pageOption.pageIndex = val
      this.loading = false
      console.log(`当前页: ${val}`)
    }
  }
}
</script>

<style lang="scss">
#add {
  overflow: hidden;
  background-color: #f8fafd;
}
.navebar {
  margin: 20px 19px 0;
  padding-top: 14px;
  padding-left: 18px;
  height: 64px;
  background-color: #fff;
}
// 表格
.box {
  margin: 20px 19px 0;
  padding: 17px 0 1px 18px;
  background-color: #fff;
}
// 按钮
.btn1 {
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: #f8fafd;
  height: 35px;
}
// 内容
.el-descriptions {
  padding: 0px 0px 0px 30px;
}
.el-dialog__footer {
  margin-top: -38px;
  padding: 15px 211px;
}
.dialog-footer {
  margin-top: 10px;
}
.btnquxiao {
  margin-right: 20px;
  height: 37px;
}
.button {
  background-color: #fbf4f0;
  border: #f8fafd;
  color: #f97138;
}
// 分页器
.block {
  margin: 20px 0;
}
.tabel {
  margin-top: 19px;
}
.btn-prev {
  margin-left: 100px;
}
// 查看详情弹窗
.view .el-dialog__title{
  font-size: 17px;
  font-weight: 600;
}
.view .el-descriptions-item__label {
  text-align: right;
}
.view .el-descriptions-item__content{
  text-align: right;
  margin-left: 15px;
}
.view .el-descriptions {
    padding: 0px 0px 0px 40px;
    margin-bottom: -20px;
}
</style>
